/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FsportDialog.java
 *
 * Created on 23.2.2010, 16:22:59
 */

package jmenu.gui;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Boner
 */
public class FsportDialog extends javax.swing.JDialog {

    private MenuPanel menuPanel;
    private long MyDate;

    /** Creates new form FsportDialog */
    public FsportDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
	this.MyDate = 0;
    }

    public void setMenuPanel(MenuPanel mp)
    {
        this.menuPanel = mp;
	this.loadData();
    }

    private void loadData()
    {
	String [][][] data= this.menuPanel.getExportData();

	Date date = new Date();
	date.setTime(date.getTime() + this.MyDate);

	DateFormat dateFormat = new SimpleDateFormat("EEE, dd.MM.yyyy");

	this.menuTextArea.append("[b]Den:[/b] " + dateFormat.format(date)+"\n");
	this.menuTextArea.append("\n");

	double E = 0;
	double B = 0;
	double S = 0;
	double T = 0;
	double Ch = 0;

	for (int i = 0; i < 7; i++)
	{

	    if (this.EmptyMeal(data[i]))
		continue;

	    this.menuTextArea.append(data[i][0][4] + "\n");

	    for (int j = 0; j < 5; j++)
	    {
		if (Integer.valueOf(data[i][j][0]) != 0 )//&& (Integer.valueOf(data[i][j][3]) != 0))
		{
		    this.menuTextArea.append("|- " + data[i][j][1] + " " + data[i][j][3] + ((data[i][j][3].equals("l"))?"ml":"g" + "\n"));
		}
	    }
	    this.menuTextArea.append("[b]Součet:[/b] " + this.getRoundedValue(Double.valueOf(data[i][5][0])) +" KJ, " + this.getRoundedValue(Double.valueOf(data[i][5][1])) + "g B, " + this.getRoundedValue(Double.valueOf(data[i][5][2])) + "g S, " + this.getRoundedValue(Double.valueOf(data[i][5][3])) + "g T, " + this.getRoundedValue(Double.valueOf(data[i][5][4])) + "mg Ch" + "\n");
	    this.menuTextArea.append("\n");
	    //System.out.println(data[i][5][0]);
	    E += Double.valueOf(data[i][5][0]);
	    B += Double.valueOf(data[i][5][1]);
	    S += Double.valueOf(data[i][5][2]);
	    T += Double.valueOf(data[i][5][3]);
	    Ch += Double.valueOf(data[i][5][4]);
	}

	this.menuTextArea.append("\n");
	this.menuTextArea.append("[b]Celkem:[/b] " + this.getRoundedValue(E) +" KJ, " + this.getRoundedValue(B) + "g B, " + this.getRoundedValue(S) + "g S, " + this.getRoundedValue(T) + "g T, " + this.getRoundedValue(Ch) + "mg Ch" + "\n");

    }

    private boolean EmptyMeal( String [][] data )
    {
	for (int j = 0; j < 5; j++)
	{
	    if (Integer.valueOf(data[j][0]) != 0 && (Integer.valueOf(data[j][3]) != 0))
	    {
	        return false;
	    }
	}
	return true;
    }

    private String getRoundedValue(double value)
    {
        int decimalPlace = 2;
        BigDecimal bd = new BigDecimal( Double.toString(value) );
        bd = bd.setScale( decimalPlace, BigDecimal.ROUND_HALF_UP );
        return bd.toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        menuTextArea = new javax.swing.JTextArea();
        DateMinusButton = new javax.swing.JButton();
        DatePlusButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        CloseButton = new javax.swing.JButton();
        CopyButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Export pro F-sport");
        setMinimumSize(new java.awt.Dimension(516, 340));
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        menuTextArea.setColumns(20);
        menuTextArea.setEditable(false);
        menuTextArea.setRows(5);
        menuTextArea.setName("menuTextArea"); // NOI18N
        jScrollPane1.setViewportView(menuTextArea);

        DateMinusButton.setText("-");
        DateMinusButton.setMaximumSize(new java.awt.Dimension(23, 23));
        DateMinusButton.setMinimumSize(new java.awt.Dimension(23, 23));
        DateMinusButton.setName("DateMinusButton"); // NOI18N
        DateMinusButton.setPreferredSize(new java.awt.Dimension(40, 15));
        DateMinusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DateMinusButtonActionPerformed(evt);
            }
        });

        DatePlusButton.setText("+");
        DatePlusButton.setMaximumSize(new java.awt.Dimension(23, 23));
        DatePlusButton.setMinimumSize(new java.awt.Dimension(23, 23));
        DatePlusButton.setName("DatePlusButton"); // NOI18N
        DatePlusButton.setPreferredSize(new java.awt.Dimension(40, 15));
        DatePlusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DatePlusButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Datum:");
        jLabel1.setName("jLabel1"); // NOI18N

        CloseButton.setText("Zavřít");
        CloseButton.setName("CloseButton"); // NOI18N
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        CopyButton.setText("Kopírovat");
        CopyButton.setToolTipText("Kopírovat do schránky");
        CopyButton.setMaximumSize(new java.awt.Dimension(40, 15));
        CopyButton.setMinimumSize(new java.awt.Dimension(40, 15));
        CopyButton.setName("CopyButton"); // NOI18N
        CopyButton.setPreferredSize(new java.awt.Dimension(40, 15));
        CopyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CopyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DateMinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DatePlusButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 297, Short.MAX_VALUE)
                        .addComponent(CopyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(CloseButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(DateMinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DatePlusButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CopyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CloseButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
	this.dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void DatePlusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DatePlusButtonActionPerformed
	this.MyDate += 86400000;
	this.menuTextArea.setText("");
	this.loadData();
    }//GEN-LAST:event_DatePlusButtonActionPerformed

    private void DateMinusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DateMinusButtonActionPerformed
	this.MyDate -= 86400000;
	this.menuTextArea.setText("");
	this.loadData();
    }//GEN-LAST:event_DateMinusButtonActionPerformed

    private void CopyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopyButtonActionPerformed
	this.menuTextArea.selectAll();
	this.menuTextArea.copy();
	this.menuTextArea.select(0, 0);
    }//GEN-LAST:event_CopyButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CloseButton;
    private javax.swing.JButton CopyButton;
    private javax.swing.JButton DateMinusButton;
    private javax.swing.JButton DatePlusButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea menuTextArea;
    // End of variables declaration//GEN-END:variables

}
